package example.service;

import example.dao.MoneyMapper;
import example.model.Money;
import example.model.MoneyExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by snksnk1091 on 2017/8/23.
 */
@Service
public class MoneyService {

    @Autowired
    private MoneyMapper moneyMapper;

    public long countMoneyByUserName(String userName){
        MoneyExample example = new MoneyExample();
        example.createCriteria().andUserNameEqualTo(userName);
        return moneyMapper.countByExample(example);
    }

    public Money getMoneyByUserName(String userName){
        MoneyExample example = new MoneyExample();
        example.createCriteria().andUserNameEqualTo(userName);
        List<Money> list =  moneyMapper.selectByExample(example);
        return list.get(list.size()-1);
    }

    public void createNewMoney(Money money){
        moneyMapper.insert(money);
    }

    public void deleteMoneyByUserName(String userName){
        MoneyExample example = new MoneyExample();
        example.createCriteria().andUserNameEqualTo(userName);
        moneyMapper.deleteByExample(example);
    }

    public List<Money> getMoneyListByUserName(String userName){
        MoneyExample example = new MoneyExample();
        example.createCriteria().andUserNameEqualTo(userName);
        return moneyMapper.selectByExample(example);
    }

    public List<Money> getAllMoneyList(){
        MoneyExample example = new MoneyExample();
        example.createCriteria();
        return moneyMapper.selectByExample(example);
    }
}
